const Router=require('koa-router');
const recommend=new Router(); 
const sqldata=require('../sqldata')
//每日推荐
recommend.get('/',async(ctx)=>{
    let sql=`select * from works,authorfoot where works.aid=authorfoot.aid`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
//古诗详情
recommend.get('/content',async(ctx)=>{
    let val=ctx.request.query
    let sql=`select * from works,authorfoot where wid=${val.wid} and works.aid=authorfoot.aid`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
//添加收藏
recommend.post('/content',async(ctx)=>{
    let val=ctx.request.body
    let sql;
    let mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from collect where wid=${val.wid} and uid='${val.uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(mydata1.length==0){
        let mydata2 = await new Promise((resolve, reject)=>{
            sql=`insert into collect (wid,uid) values (${val.wid},'${val.uid}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{
        let mydata3 = await new Promise((resolve, reject)=>{
            sql=`delete from collect where  wid=${val.wid} and uid='${val.uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    } 
    ctx.body=mydata1;
})
module.exports=recommend;